﻿
Imports System.Drawing.Graphics
Imports System.Drawing
Imports System.Drawing.BufferedGraphicsManager
Imports System.Math
Imports System.IO
Imports drawing.bozo.bozo
Imports drawing.world.environement
Imports drawing.CommonInterfaces.UI.interfaces

Public Class Form1
    Dim world As drawing.world.environement.World
    Dim refreshrate As Integer = 30
    Public motionPace As Integer = 30
    Dim xMax As Integer
    Dim yMax As Integer
    Public WithEvents t As Timer
    Public WithEvents m As Timer
    Dim graph As Graphics
    Dim bg As BufferedGraphics
    Dim pen As New Pen(Color.Blue, 10)
    Dim bozoFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\bozo.txt"
    Dim multiSelect As Boolean = False

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        t.Stop()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        xMax = Me.Panel1.Size.Width
        yMax = Me.Panel1.Size.Height
        Panel1.BackColor = Color.Black
        Panel1.AllowDrop = True
        graph = Me.Panel1.CreateGraphics
        BufferedGraphicsManager.Current.MaximumBuffer = New Size(2 * xMax + 1, 2 * yMax + 1)
        bg = BufferedGraphicsManager.Current.Allocate(graph, New Rectangle(0, 0, xMax, yMax))
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        world = New drawing.world.environement.World(New Rectangle(0, 0, xMax, yMax))
        bozosFromFile()

        '' Me.BackColor = Color.Black
        m = New Timer()
        m.Interval = MotionPace
        m.Start()

        t = New Timer()
        t.Interval = refreshrate
        t.Start()
    End Sub

    Public Sub doDraw() Handles m.Tick
        world.thick()
        world.draw(bg)


    End Sub



    Public Sub resized() Handles Me.ResizeEnd, Me.SizeChanged
        xMax = Me.Panel1.Size.Width
        yMax = Me.Panel1.Size.Height
        Panel1.BackColor = Color.Black
        Panel1.AllowDrop = True
        graph = Me.Panel1.CreateGraphics
        BufferedGraphicsManager.Current.MaximumBuffer = New Size(2 * xMax + 1, 2 * yMax + 1)
        bg = BufferedGraphicsManager.Current.Allocate(graph, New Rectangle(0, 0, xMax, yMax))
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    End Sub

    Public Sub clock() Handles t.Tick

        bg.Render(Graphics.FromHwnd(Panel1.Handle))


    End Sub

    Public Sub bozosFromFile()
        'world.clear()
        'Dim input As StreamReader
        'Try
        '    input = File.OpenText(bozoFile)
        'Catch ex As Exception

        '    Dim writer As StreamWriter = File.CreateText(bozoFile)
        '    writer.Write(My.Resources.bozo())
        '    writer.Close()
        '    MsgBox("putting bozo.txt on your desktop", MsgBoxStyle.Information)
        '    input = File.OpenText(bozoFile)
        'End Try

        'While Not input.EndOfStream
        '    Dim line As String = input.ReadLine
        '    If line.StartsWith("#") OrElse String.IsNullOrEmpty(line) Then
        '        Continue While
        '    End If
        '    Dim params As String() = line.Split(" ")
        '    Dim b As IViewable
        '    Select Case params(0)
        '        Case "bozo"
        '            ''Dim size As String() = params(2).Split(",")

        '            Dim speeds As String() = params(1).Split(",")
        '            Dim sizes As String() = params(2).Split(",")
        '            Dim where As String() = params(3).Split(",")
        '            ''b = New bozo(Integer.Parse(Size(0)), Integer.Parse(Size(1)))
        '            Dim p As New Point(Integer.Parse(where(0)), Integer.Parse(where(1)))
        '            b = New bozo.bozo(Integer.Parse(sizes(0)), Integer.Parse(sizes(1)), p, Me.world)

        '        Case Else
        '            Continue While
        '    End Select

        '    world.add(b)
        'End While
        'input.Close()
    End Sub



    Private Sub Panel1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Panel1.DoubleClick
        bozosFromFile()
    End Sub

    Private Sub Panel1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If world.selectedNumber > 0 Then
                world.MoveForSelected(e.Location)
            Else
                world.add(New bozo.bozo(50, 50, e.Location, Me.world))
            End If
        ElseIf e.Button = Windows.Forms.MouseButtons.Left Then

            For Each obj As CommonInterfaces.UI.interfaces.IClickable In world.population
                If DirectCast(obj, IShape).getClippingRcangle().Contains(e.Location) Then

                    DirectCast(obj, ISelectable).selected = Not DirectCast(obj, ISelectable).selected
                Else
                    If Not multiSelect Then

                        DirectCast(obj, ISelectable).selected = False
                    End If
                End If
            Next

        End If
    End Sub

    

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Delete Then
            Dim newpopulation As New List(Of ITickable)
            For Each obj As ITickable In world.population
                If TypeOf obj Is CommonInterfaces.UI.interfaces.ISelectable Then
                    If Not DirectCast(obj, CommonInterfaces.UI.interfaces.ISelectable).selected Then
                        newpopulation.Add(obj)
                    End If
                End If
            Next
            world.population = newpopulation
        ElseIf e.KeyCode = Keys.Control Or e.KeyCode = Keys.ControlKey Then
            multiSelect = True

        End If

    End Sub


    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.Control Or e.KeyCode = Keys.ControlKey Then
            multiSelect = False

        End If
    End Sub
End Class
